package com.xbongbong.saas.enums.business;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.base.CommunicateBaseEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 经销商跟进记录
 *
 * @author xinpeng.jiang
 * @version 1.0
 * @date 2020/12/21 11:16
 * @since V1.0
 */
@Getter
public enum DistributorCommunicateEnum {
    /**
     * 基本信息
     * other_1
     */
    BASE_INFO(CommunicateBaseEnum.BASE_INFO.getAttr(), "enum.distributorCommunicate.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 经销商名称
     * text_1
     */
    DISTRIBUTOR_NAME(CommunicateBaseEnum.MAIN_BUSINESS.getAttr(), "enum.distributorCommunicate.attrName.distributor_name", "distributorName", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    /**
     * 联系人
     * text_3
     */
    CONTACT_ARRAY(CommunicateBaseEnum.CONTACT.getAttr(), "enum.distributorCommunicate.attrName.contact_array", "contactArray", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 3, 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9970, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    /**
     * 跟进方式
     * text_4
     */
    TYPE(CommunicateBaseEnum.TYPE.getAttr(), "enum.distributorCommunicate.attrName.type", "type", FieldTypeEnum.COMBO.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    /**
     * 拜访时间
     * date_1
     */
    COMMUNICATE_TIME(CommunicateBaseEnum.COMMUNICATE_TIME.getAttr(), "enum.distributorCommunicate.attrName.communicate_time", "communicateTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    /**
     * 跟进业务
     * text_5
     */
    COMMUNICATE_BUSINESS(CommunicateBaseEnum.COMMUNICATE_BUSINESS.getAttr(), "enum.distributorCommunicate.attrName.communicate_business", "communicateBusiness", FieldTypeEnum.COMBO_LINK_BUSINESS.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 0, ScreenTypeEnum.SCREEN_RELATION.getType()),
    /**
     * 跟进内容
     * text_6
     */
    MEMO(CommunicateBaseEnum.MEMO.getAttr(), "enum.distributorCommunicate.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 1, ScreenTypeEnum.SCREEN_SHOW.getType()),
    /**
     * 语音输入
     * file_4
     */
    VIDEO(CommunicateBaseEnum.VIDEO.getAttr(), "enum.distributorCommunicate.attrName.video", "video", FieldTypeEnum.VIDEO.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 位置
     * geo_1
     */
    ADDRESS(CommunicateBaseEnum.ADDRESS.getAttr(), "enum.distributorCommunicate.attrName.address", "address", FieldTypeEnum.LOCATION.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 图片
     * file_1
     */
    IMAGES(CommunicateBaseEnum.IMAGE.getAttr(), "enum.distributorCommunicate.attrName.images", "images", FieldTypeEnum.IMAGE.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 附件
     * file_3
     */
    FILE(CommunicateBaseEnum.FILE.getAttr(), "enum.distributorCommunicate.attrName.file", "attchment", FieldTypeEnum.UPLOAD.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),

    /**
     * 关联信息
     * other_2
     */
    ASSOCIATED_INFO(CommunicateBaseEnum.ASSOCIATED_INFO.getAttr(), "enum.distributorCommunicate.attrName.associated_info", "associatedInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 下次跟进提醒
     * num_3
     */
    IF_REMIND(CommunicateBaseEnum.IS_REMIND.getAttr(), "enum.distributorCommunicate.attrName.if_remind", "ifRemind", FieldTypeEnum.SWITCH.getType(), SetTypeEnum.DISABLE_ALL.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 下次跟进时间
     * date_2
     */
    NEXT_COMMUNICATE_TIME(CommunicateBaseEnum.NEXT_COMMUNICATE_TIME.getAttr(), "enum.distributorCommunicate.attrName.next_communicate_time", "nextCommunicateTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9860, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 提醒
     * text_20
     */
    NOTIFY_TYPE(CommunicateBaseEnum.NOTIFY_TYPE.getAttr(), "enum.distributorCommunicate.attrName.notify_type", "notifyType", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9850, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 提醒时间
     * date_3
     */
    NOTIFY_TIME(CommunicateBaseEnum.NOTIFY_TIME.getAttr(), "enum.distributorCommunicate.attrName.notify_time", "notifyTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.EDIT.getCode(), 9840, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 提醒备注
     * text_21
     */
    NEXT_NOTIFY_MEMO(CommunicateBaseEnum.NEXT_NOTIFY_MEMO.getAttr(), "enum.distributorCommunicate.attrName.next_notify_memo", "nextNotifyMemo", FieldTypeEnum.TEXTAREA.getType(), 1, 0, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),

    /**
     * 系统信息
     * other_3
     */
    SYSTEM_INFO(CommunicateBaseEnum.SYSTEM_INFO.getAttr(), "enum.distributorCommunicate.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9800, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 创建人
     * creatorId
     */
    CREATOR_ID(CommunicateBaseEnum.CREATOR_ID.getAttr(), "enum.distributorCommunicate.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    /**
     * 创建时间
     * addTime
     */
    ADD_TIME(CommunicateBaseEnum.ADD_TIME.getAttr(), "enum.distributorCommunicate.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    /**
     * 更新时间
     * updateTime
     */
    UPDATE_TIME(CommunicateBaseEnum.UPDATE_TIME.getAttr(), "enum.distributorCommunicate.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9770, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),

    // 隐藏逻辑字段
    /**
     * 是否自动生成
     * num_2
     */
    AUTO_GEN(CommunicateBaseEnum.AUTO_GEN.getAttr(), "enum.distributorCommunicate.attrName.auto_gen", "autoGen", FieldTypeEnum.NUM.getType(), 1, 1, 1, ShowTypeEnum.NONE.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 员工id
     * text_11
     */
    USER_ID(CommunicateBaseEnum.USER_ID.getAttr(), "enum.distributorCommunicate.attrName.user_id", "userId", FieldTypeEnum.TEXT.getType(), 1, 1, 1, ShowTypeEnum.NONE.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 员工名字
     * text_12
     */
    USER_NAME(CommunicateBaseEnum.USER_NAME.getAttr(), "enum.distributorCommunicate.attrName.user_name", "userName", FieldTypeEnum.TEXT.getType(), 1, 1, 1, ShowTypeEnum.NONE.getCode(), 9820, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 员工头像
     * file_2
     */
    USER_AVATAR(CommunicateBaseEnum.USER_AVATAR.getAttr(), "enum.distributorCommunicate.attrName.user_avatar", "userAvatar", FieldTypeEnum.IMAGE.getType(), 1, 1, 1, ShowTypeEnum.NONE.getCode(), 9810, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 关联经销商隐藏
     * text_13
     */
    DISTRIBUTOR_NAME_LINK_TEXT(CommunicateBaseEnum.MAIN_BUSINESS_HIDE.getAttr(), "enum.distributorCommunicate.attrName.distributor_name_link_text", "distributorNameLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9800, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 关联联系人隐藏
     * text_14
     */
    CONTACT_NAME_LINK_TEXT(CommunicateBaseEnum.CONTACT_HIDE.getAttr(), "enum.distributorCommunicate.attrName.contact_name_link_text", "contactNameLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 关联访客计划隐藏
     * text_17
     */
    FINISH_COMMUNICATE_PLAN_LINK_TEXT(CommunicateBaseEnum.FINISH_COMMUNICATE_PLAN_LINK_TEXT.getAttr(), "enum.distributorCommunicate.attrName.finish_communicate_plan_link_text", "finishCommunicatePlanLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9760, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 经销商跟进业务关联businessType隐藏
     * num_1
     */
    COMMUNICATE_BUSINESS_TYPE(CommunicateBaseEnum.COMMUNICATE_BUSINESS_TYPE.getAttr(), "enum.distributorCommunicate.attrName.communicate_business_type", "communicateBusinessType", FieldTypeEnum.NUM.getType(), 3, 1, 1, ShowTypeEnum.NONE.getCode(), 9750, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * 经销商跟进业务关联名称隐藏
     * text_19
     */
    COMMUNICATE_BUSINESS_LINK_TEXT(CommunicateBaseEnum.COMMUNICATE_BUSINESS_HIDE.getAttr(), "enum.distributorCommunicate.attrName.communicate_business_link_text", "communicateBusinessLinkText", FieldTypeEnum.TEXT.getType(), 3, 1, 1, ShowTypeEnum.NONE.getCode(), 9740, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    /**
     * '@指定人员
     * array_26
     */
    AT_USERNAME(CommunicateBaseEnum.AT_USERNAME.getAttr(), "enum.distributorCommunicate.attrName.at_username", "memoLinkText", FieldTypeEnum.USER_GROUP.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9730, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ;

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    DistributorCommunicateEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                               Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 对data内的字段拼上“data.”
     *
     * @param cache
     * @return java.lang.String
     * @author zcp
     * @date 2019/3/1 18:12
     * @version v1.0
     * @since v1.0
     */
    public static String getAttrConnectData(DistributorCommunicateEnum cache) {
        return "data." + cache.getAttr();
    }

    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     *
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     * @author zcp
     * @date 2019/2/21 14:59
     * @version v1.0
     * @since v1.0
     */
    public static String getEsAttr4Keyword(DistributorCommunicateEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    public static DistributorCommunicateEnum getByAttr(String attr) {
        for (DistributorCommunicateEnum distributorCommunicateEnum : values()) {
            if (Objects.equals(distributorCommunicateEnum.getAttr(), attr)) {
                return distributorCommunicateEnum;
            }
        }
        return null;
    }

    /**
     * 跟进记录过滤字段
     *
     * @param subBusinessType
     * @return
     */
    private static List<String> detailFilterBySubBusinessType(Integer subBusinessType) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum) {
            case DETAIL_CUSTOMER_COMMUNICATE:
                return new ArrayList<>();
            case DETAIL_CONTRACT_COMMUNICATE:
                return Arrays.asList(COMMUNICATE_BUSINESS.getAttr());
            case DETAIL_OPPORTUNITY_COMMUNICATE:
                return Arrays.asList(COMMUNICATE_BUSINESS.getAttr());
            case DETAIL_QUOTATION_COMUNICATE:
                return Arrays.asList(COMMUNICATE_BUSINESS.getAttr());
            case DETAIL_CONTACT_COMMUNICATE:
                return Arrays.asList(CONTACT_ARRAY.getAttr());
            default:
                break;
        }
        return new ArrayList<>();
    }

    /**
     * 跟进记录保留字段
     *
     * @param subBusinessType
     * @return
     */
    private static List<String> detailRetainBySubBusinessType(Integer subBusinessType) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum) {
            case DETAIL_CUSTOMER_COMMUNICATE:
                return Arrays.asList(COMMUNICATE_BUSINESS.getAttr(), CONTACT_ARRAY.getAttr(), IMAGES.getAttr(), FILE.getAttr());
            case DETAIL_CONTRACT_COMMUNICATE:
                return Arrays.asList(CONTACT_ARRAY.getAttr(), IMAGES.getAttr(), FILE.getAttr());
            case DETAIL_OPPORTUNITY_COMMUNICATE:
                return Arrays.asList(CONTACT_ARRAY.getAttr(), IMAGES.getAttr(), FILE.getAttr());
            case DETAIL_QUOTATION_COMUNICATE:
                return Arrays.asList(CONTACT_ARRAY.getAttr(), IMAGES.getAttr(), FILE.getAttr());
            case DETAIL_CONTACT_COMMUNICATE:
                return Arrays.asList(COMMUNICATE_BUSINESS.getAttr(), IMAGES.getAttr(), FILE.getAttr());
            default:
                break;
        }
        return new ArrayList<>();
    }

    /**
     * 是否是需要过滤删除的字段
     *
     * @param attr
     * @param subBusinessType
     * @param required
     * @param fieldType
     * @return
     */
    public static Boolean detailFilter(String attr, Integer subBusinessType, Integer required, Integer fieldType) {
        //客户和备注直接过滤
        if (Objects.equals(DISTRIBUTOR_NAME.getAttr(), attr) || Objects.equals(MEMO.getAttr(), attr)) {
            return true;
        }
        //过滤本业务
        if (detailFilterBySubBusinessType(subBusinessType).contains(attr)) {
            return true;
        }
        //强制保留
        if (detailRetainBySubBusinessType(subBusinessType).contains(attr)) {
            return false;
        }
        if (Objects.equals(fieldType, FieldTypeEnum.SEPARATOR.getType())) {
            return true;
        }
        //过滤非必填
        return Objects.equals(required, 0);
    }

    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     *
     * @param attr 字段名
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/4 15:31
     * @version v1.0
     * @since v1.0
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, DISTRIBUTOR_NAME.attr)) {
            return DISTRIBUTOR_NAME_LINK_TEXT.attr;
        } else if (Objects.equals(attr, COMMUNICATE_BUSINESS.attr)) {
            return COMMUNICATE_BUSINESS_LINK_TEXT.attr;
        } else if (Objects.equals(attr, CONTACT_ARRAY.attr)) {
            return CONTACT_NAME_LINK_TEXT.attr;
        }
        return attr;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }
}
